package cn.edu.ujn.finaltest.controller;

import java.sql.Timestamp;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.ujn.finaltest.dao.BaseDict;
import cn.edu.ujn.finaltest.dao.Customer;
import cn.edu.ujn.finaltest.dao.User;
import cn.edu.ujn.finaltest.service.IBaseDictService;
import cn.edu.ujn.finaltest.service.ICustomerService;
import cn.edu.ujn.finaltest.util.Page;

@Controller
public class CustomerController {
	// 依赖注入
		@Autowired
		private ICustomerService customerService;
		@Autowired
		private IBaseDictService baseDictService;
		// 客户来源
		@Value("${customer.from.type}")
		private String FROM_TYPE;
		// 客户所属行业
		@Value("${customer.industry.type}")
		private String INDUSTRY_TYPE;
		// 客户级别
		@Value("${customer.level.type}")
		private String LEVEL_TYPE;
		/**
		 *  客户列表
		 */
		@RequestMapping(value = "/customer/list.action")
		public String list(@RequestParam(defaultValue="1")Integer page,
				@RequestParam(defaultValue="10")Integer rows, 
				String custName, String custSource, String custIndustry,
				String custLevel, Model model) {
			// 条件查询所有客户
			// 客户来源
			List<BaseDict> fromType = baseDictService.selectBaseDictByTypeCode(FROM_TYPE);
			// 客户所属行业
			List<BaseDict> industryType = baseDictService.selectBaseDictByTypeCode(INDUSTRY_TYPE);
			// 客户级别
			List<BaseDict> levelType = baseDictService.selectBaseDictByTypeCode(LEVEL_TYPE);
			//查询数据
			Page<Customer> customers = customerService.findCustomerList(page, rows, custName, custSource, custIndustry,custLevel);
			model.addAttribute("page", customers);
			// 添加参数
			model.addAttribute("fromType", fromType);
			model.addAttribute("industryType", industryType);
			model.addAttribute("levelType", levelType);

			model.addAttribute("custName", custName);
			model.addAttribute("custSource", custSource);
			model.addAttribute("custIndustry", custIndustry);
			model.addAttribute("custLevel", custLevel);
			
			return "customer";
		}
		
		/**
		 * 创建客户
		 */
		@RequestMapping("/customer/create.action")
		@ResponseBody
		public String customerCreate(Customer customer, HttpSession session) {
		    User user = (User) session.getAttribute("USER_SESSION");
		    customer.setCust_create_id(user.getUser_id());
		    // 创建Date对象
		    Date date = new Date();
		    Timestamp timeStamp = new Timestamp(date.getTime());
		    customer.setCust_createtime(timeStamp);
		    int rows = customerService.insertSelective(customer);
		    if(rows > 0){
		        return "OK";
		    }else{
		        return "FAIL";
		    }
		}
		
		/**
		 * 通过id获取客户信息
		 */
		@RequestMapping("/customer/getCustomerById.action")
		@ResponseBody
		public Customer getCustomerById(Integer id) {
		    Customer customer = customerService.selectByPrimaryKey(id);
		    return customer;
		}
		
		/**
		 * 更新客户
		 */
		@RequestMapping("/customer/update.action")
		@ResponseBody
		public String customerUpdate(Customer customer) {
		    int rows = customerService.updateByPrimaryKeySelective(customer);
		    System.out.println(rows);
		    if(rows > 0){
		        return "OK";
		    }else{
		        return "FAIL";
		    }
		}
		/**
		 * 删除客户
		 */
		@RequestMapping("/customer/delete.action")
		@ResponseBody
		public String customerDelete(Integer id) {
		    int rows = customerService.deleteByPrimaryKey(id);
		    if(rows > 0){			
		        return "OK";
		    }else{
		        return "FAIL";			
		    }
		}


}
